//
//  ViewController.m
//  MovingBall
//
//  Created by iD Student on 7/25/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize ball;
@synthesize position;

- (void)viewDidLoad
{
    [super viewDidLoad];
    bounce = 0;
    [NSTimer scheduledTimerWithTimeInterval:(0.05) target:(self) selector:@selector (moveBall) userInfo:nil repeats:(YES)];
    velocity = 7;
    
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [self setBall:nil];
    [self setPosition:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (void) moveBall
{
    if(ball.center.x >= 270){
        bounce = 1;
    }else if(ball.center.x <= 50){
        bounce = 0;
    }
    
    switch(bounce){
        case 0:
            [ball setCenter:CGPointMake(ball.center.x+velocity, ball.center.y)];
            ball.transform = CGAffineTransformMakeScale(1, 1);
            break;
        case 1:
            [ball setCenter:CGPointMake(ball.center.x-velocity, ball.center.y)];
            ball.transform = CGAffineTransformMakeScale(-1, 1);
            break;
    }
    
    if(ball.center.y >= 400){
        bounce2 = 1;
    }else if(ball.center.y <= 40){
        bounce2 = 0;
    }
    switch(bounce2){
        case 0:
            [ball setCenter:CGPointMake(ball.center.x, ball.center.y+velocity)];
            break;
        case 1:
            [ball setCenter:CGPointMake(ball.center.x, ball.center.y-velocity)];
            break;
    }
    [position setText:[NSString stringWithFormat:@"(%0.2f, %0.2f)", ball.center.x, ball.center.y]];
  
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
